package com.lhf.planClass.service.impl;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.lhf.enumfile.Status;
import com.lhf.error.ErrorMessageVo;
import com.lhf.planClass.mapper.PlanClassVoMapper;
import com.lhf.planClass.model.*;
import com.lhf.planClass.service.PlanClassVoService;
import com.lhf.util.AllrecordsVo;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author 尐痞猫
 * @date2021-06-24 10:23
 */
@Service
public class PlanClassVoServiceImpl implements PlanClassVoService {

    @Autowired
    private PlanClassVoMapper planClassVoMapper;

    /*
    分页查询
     */
    @Override
    public ResultVo queryPage(PlanClassSelectVo planClassSelectVo) {
        //查询数据
        List<PlanClassVo>  list = planClassVoMapper.queryPage(planClassSelectVo);
        int count = planClassVoMapper.queryCount();
        for (PlanClassVo planClassVo:list) {
            planClassVo.setStatusName(Status.getStatusName(planClassVo.getStatus()));
        }
        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setPageIndex(planClassSelectVo.getPageIndex());
        allrecordsVo.setPageSize(planClassSelectVo.getPageSize());
        allrecordsVo.setTotalSize(count);
        allrecordsVo.resettotalNum(count);
        allrecordsVo.setDataList(list);

        return ResultBuliderVo.success(allrecordsVo);
    }
    /*
        添加
         */
    @Override
    @Transactional
    public ResultVo add(PlanCLassAddVo planClassSelectVo) {
        //查询编号不能重复
        int count = planClassVoMapper.queryByCode(planClassSelectVo.getCode());
        if(count!=0){
            return ResultBuliderVo.error(ErrorMessageVo.BIANMAUNENGCHONGFU);
        }
        //查询名称不能重复
        int count2= planClassVoMapper.queryByName(planClassSelectVo.getName());
        if(count2!=0){
            return ResultBuliderVo.error(ErrorMessageVo.MINGZIBUNENGCHONGFU);
        }
        PlanClassVo planClassVo = new PlanClassVo();
        BeanUtils.copyProperties(planClassSelectVo,planClassVo);
        Date now = new Date();
        planClassVo.setCreateName("李四");
        planClassVo.setCreateTime(now);
        planClassVo.setUpdateName("李四");
        planClassVo.setUpdateTime(now);
        planClassVo.setStatus(planClassSelectVo.getStatus()?1:0);
        int a = planClassVoMapper.insert(planClassVo);
        return ResultBuliderVo.success(null);
    }
    /*
        修改
         */
    @Transactional
    @Override
    public ResultVo update(@Valid PlanClassUpdateVo planClassUpdateVo) {
        //验证之前的状态是不是禁用的
        PlanClassVo planClassVo1 = planClassVoMapper.queryById(planClassUpdateVo.getId());
        if(planClassVo1==null){
            return ResultBuliderVo.error(ErrorMessageVo.XIUGAIDUIXIANGBUCUNZAI);
        }
        if(planClassVo1.getStatus()!=0){
            return ResultBuliderVo.error(ErrorMessageVo.ZHIYOUJINYONGCAINENGXIUGAI);
        }

        if(!planClassVo1.getName().equals(planClassUpdateVo.getName())){
            //查询名称不能重复
            int count2= planClassVoMapper.queryByName(planClassUpdateVo.getName());
            if(count2!=0){
                return ResultBuliderVo.error(ErrorMessageVo.MINGZIBUNENGCHONGFU);
            }
        }
        PlanClassVo planClassVo = new PlanClassVo();
        BeanUtils.copyProperties(planClassUpdateVo,planClassVo);
        Date now = new Date();
        planClassVo.setUpdateName("王五");
        planClassVo.setUpdateTime(now);
        planClassVo.setStatus(planClassUpdateVo.getStatus()?1:0);

        int a = planClassVoMapper.update(planClassVo);
        return ResultBuliderVo.success(null);
    }

    @Override
    public ResultVo queryById(Integer id) {
        PlanClassVo planClassVo = planClassVoMapper.queryById(id);
        if(planClassVo==null){
            return ResultBuliderVo.error(ErrorMessageVo.XIUGAIDUIXIANGBUCUNZAI);
        }
        planClassVo.setStatusName(Status.getStatusName(planClassVo.getStatus()));
        return ResultBuliderVo.success(planClassVo);
    }

    @Override
    @Transactional
    public ResultVo updateStatus(PlanClasUpdateStatusVo planClasUpdateStatusVo) {
        PlanClassVo planClassVo = planClassVoMapper.queryById(planClasUpdateStatusVo.getId());
        if(planClassVo==null){
            return ResultBuliderVo.error(ErrorMessageVo.XIUGAIDUIXIANGBUCUNZAI);
        }

        PlanClassVo planClassVo1 = new PlanClassVo();
        BeanUtils.copyProperties(planClasUpdateStatusVo,planClassVo1);
        Date now = new Date();
        planClassVo1.setUpdateName("王五");
        planClassVo1.setUpdateTime(now);
        planClassVo1.setStatus(planClasUpdateStatusVo.getStatus()?1:0);
        planClassVoMapper.updateStatus(planClassVo1);
        return ResultBuliderVo.success(null);
    }
}
